package servicedevmap

import "emsctrl/public"

var (
	PCS_YGGL        = "yggl"
	PCS_WGGL        = "wggl"
	PCS_OPEN        = "open"
	PCS_CONNECT_SET = "connectset"
	PCS_CLOSE       = "close"
	PCS_RESET       = "reset"
	PCS_REMOTE_SET  = "remoteset"
	PCS_CONNECTON   = "connecton"
	PCS_CONNECTOFF  = "connectOff"
	PCS_FAULT       = "fault"
	PCS_ALARM       = "alarm"
	PCS_RUNSTATE    = "runstate"
	PCS_STOPSTATE   = "stopstate"
	PCS_IDLESTATE   = "idlestate"
	PCS_REMOTESTATE = "remotestate"
	PCS_EMERGSTOP   = "emergstop"
)

type pcsdevMap struct {
	yggl        float32
	wggl        float32
	open        int
	connectset  int
	close       int
	reset       int
	remoteset   int
	connnecton  int
	connectoff  int
	fault       int
	alarm       int
	runstate    int
	stopstate   int
	idlestate   int
	remotestate int
	emegstop    int
}

var pcsMap map[string]map[string]string

func init() {
	pcsMap = make(map[string]map[string]string)
}

func (p *pcsdevMap) Analysis_string(info []string) {
	var ytype string
	var key string
	var mapv string
	for i, s := range info {
		switch i {
		case 0:
			break
		case 1:
			ytype = s
		case 2:
			key = s
		case 3:
			mapv = s
		}
	}

	if _, ok := pcsMap[ytype]; ok {

	} else {
		pcsMap[ytype] = make(map[string]string)
	}
	pcsMap[ytype][key] = mapv
}

func (p *pcsdevMap) upGradeDevData(index int, devtype, devname, paratype string, datum public.DataVal) {
	reg := StationMap[index][devtype][devname]

	if _, ok := pcsMap[paratype]; ok {
		switch pcsMap[paratype][datum.Key] {
		case PCS_YGGL, PCS_WGGL, PCS_OPEN, PCS_CLOSE, PCS_RESET,
			PCS_CONNECTON, PCS_CONNECTOFF, PCS_FAULT, PCS_REMOTE_SET,
			PCS_ALARM, PCS_RUNSTATE, PCS_STOPSTATE, PCS_CONNECT_SET,
			PCS_IDLESTATE, PCS_REMOTESTATE, PCS_EMERGSTOP:
			if reg.RegMap == nil {
				reg.RegMap = make(map[string]RegInfo)
			} else {

			}
			if _, ok1 := reg.RegMap[pcsMap[paratype][datum.Key]]; ok1 {
				info := reg.RegMap[pcsMap[paratype][datum.Key]]
				info.Val = datum.Val
				info.Addr = datum.RegAddr
				info.PType = paratype
				reg.RegMap[pcsMap[paratype][datum.Key]] = info
			} else {
				reg.RegMap[pcsMap[paratype][datum.Key]] = RegInfo{Key: datum.Key, Val: datum.Val, Addr: datum.RegAddr, PType: paratype}
			}

		default:

		}
	}
	StationMap[index][devtype][devname] = reg
}

func (p *pcsdevMap) upGradeCurrPolicyPara(devtype, devName, paratype string) {
	*p = pcsdevMap{}
	for _, m := range StationMap {
		if _, ok := m[devtype][devName]; ok {
			reg := m[devtype][devName]
			for s, info := range reg.RegMap {
				switch s {
				case PCS_YGGL:
					p.yggl += info.Val
				case PCS_WGGL:
					p.wggl += info.Val
				case PCS_OPEN:
					p.open |= int(info.Val)
				case PCS_CONNECT_SET:
					p.connectset = int(info.Val)
				case PCS_CLOSE:
					p.close |= int(info.Val)
				case PCS_RESET:
					p.reset |= int(info.Val)
				case PCS_REMOTE_SET:
					p.remoteset = int(info.Val)
				case PCS_CONNECTON:
					p.connnecton |= int(info.Val)
				case PCS_CONNECTOFF:
					p.connectoff |= int(info.Val)
				case PCS_FAULT:
					p.fault |= int(info.Val)
				case PCS_ALARM:
					p.alarm |= int(info.Val)
				case PCS_RUNSTATE:
					p.runstate |= int(info.Val)
				case PCS_STOPSTATE:
					p.stopstate |= int(info.Val)
				case PCS_IDLESTATE:
					p.idlestate |= int(info.Val)
				case PCS_REMOTESTATE:
					p.remotestate |= int(info.Val)
				case PCS_EMERGSTOP:
					p.emegstop |= int(info.Val)
				}
			}
		}
	}

	G_curr_policy.RemoteMetering.StationActivePower = p.yggl
	G_curr_policy.RemoteMetering.StationReactivePower = p.wggl
	G_curr_policy.RemoteSignal.PcsAlarmState = p.alarm
	G_curr_policy.RemoteSignal.PcsOpenState = p.open
	G_curr_policy.RemoteSignal.PcsRunState = p.runstate
	G_curr_policy.RemoteSignal.PcsGridConnState = p.connnecton
	G_curr_policy.RemoteSignal.PcsRemoteState = p.remotestate
	G_curr_policy.RemoteSignal.PcsFaultState = p.fault
	G_curr_policy.RemoteSignal.PcsEmegstop = p.emegstop
}
